from django.db import models
from KeHu.models import KeHu,KeHuzilao
from tinymce import models as tinymce_models
from django import forms
import os
import uuid
import requests
from bs4 import BeautifulSoup
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.contrib.auth.models import User
from ChanPin.models import Chanpin,Dingdan

# Create your models here.


class Xueyuan_zongbiao(models.Model):
    """学员及资料资料"""
    jiguan_choices = [("广东", "广东"), ("河南", "河南"), ("山东", "山东"), ("河北", "河北"),
                      ("山西", "山西"), ("安徽", "安徽"), ("江西", "江西"),
                      ("福建", "福建"), ("湖南", "湖南"), ("湖北", "湖北"), ("广西", "广西"), ("海南", "海南"),
                      ("云南", "云南"), ("贵州", "贵州"), ("重庆", "重庆"),
                      ("四川", "四川"), ("陕西", "陕西"), ("甘肃", "甘肃"),
                      ("青海", "青海"), ("宁夏", "宁夏"), ("内蒙古", "内蒙古"), ("江苏", "江苏")]
    wenhuachengdu_choices = [("高中", "高中"), ("中专", "中专"), ("大专", "大专"), ("本科", "本科")]

    kehu = models.OneToOneField(KeHu, on_delete=models.CASCADE,verbose_name='关联客户')
    xueyuan_jinbantime = models.DateTimeField(verbose_name='学员首次进班时间', blank=True, null=True,)
    chengjiao_laoshi = models.ForeignKey(User,verbose_name='成交老师',blank=True,null=True,on_delete=models.SET_NULL)
    baomingtime=models.DateTimeField(verbose_name='报名时间', blank=True, null=True)
    shenfenzheng = models.CharField(max_length=100, verbose_name='身份证号', blank=True, null=True)
    wenhuachengdu=models.CharField(max_length=100, verbose_name='文化程度',choices=wenhuachengdu_choices, blank=True, null=True)
    jiguan = models.CharField(max_length=100, verbose_name='籍贯', blank=True, null=True,choices=jiguan_choices)
    xueyuan_zhengjian_opentime= models.DateTimeField(verbose_name='学员资格证开取时间', blank=True, null=True)
    xueyuan_zhengjian_endtime = models.DateTimeField(verbose_name='学员资格证到期时间', blank=True, null=True)
    xueyuan_zhengjian_img = models.ImageField(verbose_name='学员证书图片', upload_to='xueyuan_zhengjian_img/', blank=True,)
    xueyuan_zhengjian_shouming = tinymce_models.HTMLField(verbose_name='学员资格证说明', blank=True)
    beizhu = tinymce_models.HTMLField(verbose_name='备注', blank=True, null=True)
    xueyuan_xuehao = models.CharField(verbose_name='学员学号', max_length=100, blank=True, null=True)
    xueyuan_kaoshenghao = models.CharField(verbose_name='学员考生的考号', max_length=100, blank=True, null=True)
    #xueyuan_dingdan=models.ManyToManyField(Dingdan,verbose_name='关联订单', blank=True,null=True)
    xueyuan_ziliao = models.OneToOneField(
        'Xueyuan_Zilao',
        verbose_name='学员资料',
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name="zongbiao"  # 从 Xueyuan_Zilao 反向到 Xueyuan_zongbiao 的关系名称
    )


    def __str__(self):
        return self.kehu.username

    class Meta:
        verbose_name = '正式学员总表'
        verbose_name_plural = '正式学员总表管理'

class Xueyuan_Zilao(models.Model):
    """客户资料 学历"""
    xueyuan = models.OneToOneField(
        Xueyuan_zongbiao,
        on_delete=models.CASCADE,
        null=True,
        related_name="ziliao"  # 从 Xueyuan_zongbiao 反向到 Xueyuan_Zilao 的关系名称
    )
    xm = models.CharField(verbose_name='姓名', max_length=22, default='', blank=True)
    xb = models.CharField(verbose_name='性别', max_length=22, default='', blank=True)
    zzmm = models.CharField(verbose_name='政治面貌', max_length=22, default='', blank=True)
    zjlx = models.CharField(verbose_name='证件类型', max_length=22, default='', blank=True)
    zjhm = models.CharField(verbose_name='证件号码', max_length=22, default='', blank=True)
    csnyr = models.DateField(verbose_name='出生年月日',null=True,blank=True)
    mz = models.CharField(verbose_name='民族', max_length=22, default='', blank=True)
    zycc = models.CharField(verbose_name='专业层次', max_length=23, default='', blank=True)
    zydm = models.CharField(verbose_name='专业代码', max_length=24, default='', blank=True)
    zymc = models.CharField(verbose_name='专业名称', max_length=25, default='', blank=True)
    xslx = models.CharField(verbose_name='学生类型', max_length=26, default='', blank=True)
    rxcscj = models.CharField(verbose_name='入学测试成绩', max_length=27, default='', blank=True)
    zzzt_choices = [
        ("国家机关党群组织企事业单位负责人", "国家机关党群组织企事业单位负责人"),
        ("专业技术人员", "专业技术人员"),
        ("办事人员和有关人员", "办事人员和有关人员"),
        ("商业工作人员", "商业工作人员"),
        ("服务性工作人员", "服务性工作人员"),
        ("农林牧渔水利业人员", "农林牧渔水利业人员"),
        ("工人", "工人"),
        ("待业人员", "待业人员"),
        ("学生", "学生"),
                    ]
    zzzt = models.CharField(verbose_name='在职状况', max_length=28, default='', blank=True,choices=zzzt_choices)
    xz = models.CharField(verbose_name='学制', max_length=29, default='', blank=True,choices=[("2.5年","2.5年"),("3年","3年")])
    fbqk = models.CharField(verbose_name='分布情况', max_length=30, default='', blank=True,choices=[("本地","本地"),("外地","外地")])
    hyzt = models.CharField(verbose_name='婚姻状况', max_length=31, default='', blank=True,choices=[("未婚","未婚"),("已婚","已婚"),("丧偶","丧偶"),("离婚","离婚"),("其他","其他")])
    xfly = models.CharField(verbose_name='学费来源', max_length=32, default='', blank=True,choices=[("自费","自费"),("部分公费","部分公费"),("公费","公费"),("其他","其他")])
    hkxz = models.CharField(verbose_name='户口性质', max_length=33, default='', blank=True,choices=[("农业","农业"),("非农业","非农业")])
    jgsf = models.CharField(verbose_name='籍贯（省）', max_length=34, default='', blank=True)
    jgs = models.CharField(verbose_name='籍贯（地市县）', max_length=35, default='', blank=True)
    hkszd = models.CharField(verbose_name='户口所在地', max_length=36, default='',blank=True)
    tel = models.CharField(verbose_name='手机号码', max_length=37, default='', blank=True)
    gdtel = models.CharField(verbose_name='固定电话', max_length=38, default='', blank=True)
    yx = models.CharField(verbose_name='邮箱', max_length=39, default='', blank=True)
    txdz = models.CharField(verbose_name='通信地址', max_length=40, default='', blank=True)
    yb = models.CharField(verbose_name='邮编', max_length=41, default='', blank=True)
    sfdd = models.CharField(verbose_name='是否电大毕业', max_length=42, default='', blank=True,choices=[("是","是"),("否","否")])
    yxlcc_choices= [
        ("职高中专技校等同等学历", "职高中专技校等同等学历"),
        ("大专", "大专"),
        ("高中", "高中"),
        ("学士", "学士"),
        ("研究生", "研究生"),
        ("硕士", "硕士"),
        ("博士", "博士"),
        ("本科", "本科"),
    ]
    yxlcc = models.CharField(verbose_name='原学历层次', max_length=43, default='', blank=True,choices=yxlcc_choices)
    ybyxx = models.CharField(verbose_name='原毕业学校', max_length=44, default='', blank=True)
    bysj = models.DateField(verbose_name='毕业时间',null=True,blank=True)
    yxk = models.CharField(verbose_name='原学科', max_length=45, default='', blank=True)
    yxkml = models.CharField(verbose_name='原学科门类', max_length=47, default='', blank=True)
    yxlxxlx = models.CharField(verbose_name='原学历学习类型', max_length=48, default='', blank=True)
    xxlbyzszy = models.CharField(verbose_name='原学历所学专业', max_length=49, default='', blank=True)
    xxlbyzsbh = models.CharField(verbose_name='原学历毕业证书编号', max_length=50, default='', blank=True)
    yxlzmcl_yxlzmcl = [
        ("学历认证报告", "学历认证报告"),
        ("留学认证报告", "留学认证报告"),
        ("中学中职中技毕业证网查", "中学中职中技毕业证网查"),
        ("学历证书电子注册备案表在线验证码", "学历证书电子注册备案表在线验证码"),
    ]
    yxlzmcl = models.CharField(verbose_name='原学历证明材料', max_length=51, default='', blank=True,choices=yxlzmcl_yxlzmcl)
    zmclbh = models.CharField(verbose_name='证明材料编号', max_length=52, default='', blank=True)
    yxlxm = models.CharField(verbose_name='原学历姓名', max_length=53, default='', blank=True)
    yxlzjlx_yxlzjlx = [
        ("自考", "自考"),
        ("成人高等教育", "成人高等教育"),
        ("电大（开放教育）", "电大（开放教育）"),
        ("普通高等教育", "普通高等教育"),
        ("网络教育", "网络教育"),
        ]
    yxlzjlx = models.CharField(verbose_name='原学历证件类型', max_length=54, default='', blank=True,choices=yxlzjlx_yxlzjlx)
    yxlzjhm = models.CharField(verbose_name='原学历证件号码', max_length=55, default='', blank=True)

    def __str__(self):
        return self.xueyuan.kehu.username +" - "+ self.xm

    class Meta:
        verbose_name = '学员资料'
        verbose_name_plural = '正式学员资料'

class Xueyuan_tezhong_Zilao(models.Model):
    xueyuan = models.OneToOneField(Xueyuan_zongbiao,on_delete=models.CASCADE,null=True)
    baokaofangshi_choices = [
        ("本地户籍", "本地户籍"),
        ("居住证", "居住证"),
        ("社保", "社保"),
        ("劳动合同", "劳动合同"),
        ("在校证明", "在校证明"),
    ]
    baokaofangshi = models.IntegerField(verbose_name='报考方式', default='本地户籍', blank=True,choices=baokaofangshi_choices,null=True)



class Banji(models.Model):
    name = models.CharField(max_length=20)
    xueyuan = models.ManyToManyField('Xueyuan_zongbiao', blank=True, verbose_name='班级学员')
    banji_leader = models.ManyToManyField(User, verbose_name='班级负责人', blank=True)
    banji_leader_tel = models.CharField(max_length=20, verbose_name='班级负责人电话', blank=True, null=True)
    banji_leibie_choices = [("学历", "学历"), ("特种作业", "特种作业"), ("职业资格证", "职业资格证"), ("其他", "其他")]
    banji_leibie = models.CharField(max_length=20, verbose_name='班级类型', choices=banji_leibie_choices,default="其他")
    banji_chanpin = models.ManyToManyField(Chanpin, verbose_name='班级课程', blank=True)
    open_time = models.DateField(verbose_name='开班时间', blank=True, null=True)
    end_time = models.DateField(verbose_name='结束时间', blank=True, null=True)
    shouming = tinymce_models.HTMLField(verbose_name='说明', blank=True, null=True)
    beizhu = tinymce_models.HTMLField(verbose_name='备注', blank=True, null=True)
    ruxue_time = models.DateField(verbose_name='入学批次（年月）', blank=True, null=True)
    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '班级'
        verbose_name_plural = '班级管理'

class xueyuan_jiaowu_genjinlog(models.Model):
    """学员及资料资料"""
    kehu = models.ForeignKey(Xueyuan_zongbiao, on_delete=models.CASCADE,verbose_name='关联正式学员')
    gengjin_time= models.DateTimeField(verbose_name='跟进时间', blank=True, null=True)
    addlog_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='添加人', blank=True, null=True)
    gengjin_content = tinymce_models.HTMLField(verbose_name='跟进内容', blank=True, null=True)

    def __str__(self):
        return self.kehu.kehu.username

    class Meta:
        verbose_name = '学员教务跟进记录'
        verbose_name_plural = '学员教务跟进记录管理'

class xueyuan_xieyinfo(models.Model):
    """学员协议信息"""
    xueyuan = models.OneToOneField(Xueyuan_zongbiao, on_delete=models.CASCADE,verbose_name='关联正式学员')
    xieyi_file = models.FileField(upload_to='xieyi/%Y/%m/%d', verbose_name='学员协议', blank=True, null=True)
    xieyi_time = models.DateTimeField(verbose_name='学员协议时间', blank=True, null=True)
    faqilaoshi = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='发起老师', blank=True, null=True)